package osb.map.map;

import java.awt.List;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

/**
 * 轨迹记录类
 * 
 * @author Administrator
 * 
 */

// 示例
/*
 * GeoPoint p1 = new GeoPoint((int) (26.033754 * 1E6), (int) (119.215354 *
 * 1E6)); GeoPoint p2 = new GeoPoint((int) (26.029014 * 1E6), (int) (119.220421
 * * 1E6)); GeoPoint p3 = new GeoPoint((int) (26.027163 * 1E6), (int)
 * (119.217905 * 1E6)); Map_Track.OpenTrack(0,p1); Map_Track.OpenTrack(0,p2);
 * Map_Track.OpenTrack(0,p3);
 */

public class Map_Track extends Overlay {

	static public HashMap<String, Map_Track> static_Map_Track = new HashMap<String, Map_Track>();

	private GeoPoint gp1;
	private GeoPoint gp2;

	static GeoPoint LastPoint_me = null; // 上一个传入的点 我的
	static GeoPoint LastPoint_xiaolv1 = null; // 上一个传入的点 小绿1
	static GeoPoint LastPoint_xiaolv2 = null; // 上一个传入的点 小绿2
	static GeoPoint LastPoint_xiaolv3 = null; // 上一个传入的点 小绿3
	static GeoPoint LastPoint_xiaolv4 = null; // 上一个传入的点 小绿4
	static GeoPoint LastPoint_xiaolv5 = null; // 上一个传入的点 小绿5
	static GeoPoint LastPoint_xiaolv6 = null; // 上一个传入的点 小绿6


	private static ArrayList<Map_Track> geopoint_me_list = new ArrayList<Map_Track>();
	private static ArrayList<Map_Track> geopoint_xiaolv1_list = new ArrayList<Map_Track>();
	private static ArrayList<Map_Track> geopoint_xiaolv2_list = new ArrayList<Map_Track>();
	private static ArrayList<Map_Track> geopoint_xiaolv3_list = new ArrayList<Map_Track>();
	private static ArrayList<Map_Track> geopoint_xiaolv4_list = new ArrayList<Map_Track>();
	private static ArrayList<Map_Track> geopoint_xiaolv5_list = new ArrayList<Map_Track>();
	private static ArrayList<Map_Track> geopoint_xiaolv6_list = new ArrayList<Map_Track>();

	static int count_error = 0; // 计算变化错误的次数 辅助判断是否定位在跳跃

	/*
	 * 调用方法 if (Public_Operate_And_Data.OpenTrack_me) Map_Track.OpenTrack(0,
	 * pt);
	 */
	public static void OpenTrack(int type, GeoPoint NewPoint) {

		switch (type) {
		case 0: // 我的轨迹
			if (LastPoint_me != null) {
				// 2次的改变距离在500米以内
				// y我的位置由于设备原因通常定位不稳定
				// 所以需要添加判断
				if (Compute_Distance(NewPoint, LastPoint_me)) {
					Map_Track mTrack = new Map_Track(LastPoint_me, NewPoint);
					Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
					geopoint_me_list.add(mTrack);
					LastPoint_me = NewPoint;
				} else {
					if (count_error > 3) {
						LastPoint_me = NewPoint; // 大于3次。重新设置起始点
						count_error = 0;
					}
					count_error++;
				}
			} else {
				LastPoint_me = NewPoint;
			}

			break;
		case 1: // 小绿1
			if (LastPoint_xiaolv1 != null) {
				Map_Track mTrack = new Map_Track(LastPoint_xiaolv1, NewPoint);
				Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
				geopoint_xiaolv1_list.add(mTrack);
			}
			LastPoint_xiaolv1 = NewPoint;
			break;
		case 2:
			if (LastPoint_xiaolv2 != null) {
				Map_Track mTrack = new Map_Track(LastPoint_xiaolv2, NewPoint);
				Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
				geopoint_xiaolv2_list.add(mTrack);
			}
			LastPoint_xiaolv2 = NewPoint;
			break;
		case 3:
			if (LastPoint_xiaolv3 != null) {
				Map_Track mTrack = new Map_Track(LastPoint_xiaolv3, NewPoint);
				Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
				geopoint_xiaolv3_list.add(mTrack);
			}
			LastPoint_xiaolv3 = NewPoint;
			break;
		case 4:
			if (LastPoint_xiaolv4 != null) {
				Map_Track mTrack = new Map_Track(LastPoint_xiaolv4, NewPoint);
				Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
				geopoint_xiaolv4_list.add(mTrack);
			}
			LastPoint_xiaolv4 = NewPoint;
			break;
		case 5:
			if (LastPoint_xiaolv5 != null) {
				Map_Track mTrack = new Map_Track(LastPoint_xiaolv5, NewPoint);
				Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
				geopoint_xiaolv5_list.add(mTrack);
			}
			LastPoint_xiaolv5 = NewPoint;
			break;
		case 6:
			if (LastPoint_xiaolv6 != null) {
				Map_Track mTrack = new Map_Track(LastPoint_xiaolv6, NewPoint);
				Map.Public_Static_Map.mapView.getOverlays().add(mTrack);
				geopoint_xiaolv6_list.add(mTrack);
			}
			LastPoint_xiaolv6 = NewPoint;
			break;

		default:
			break;
		}
	}

	/**
	 * 清除轨迹
	 * @param type
	 */
	public static void RemoveTrack(int type) {
		switch (type) {
		case 0:
			for (int i = 0; i < geopoint_me_list.size(); i++) {
				if (geopoint_me_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_me_list.get(i));
			}
			break;
		case 1:
			for (int i = 0; i < geopoint_xiaolv1_list.size(); i++) {
				if (geopoint_xiaolv1_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_xiaolv1_list.get(i));
			}
			break;
		case 2:
			for (int i = 0; i < geopoint_xiaolv2_list.size(); i++) {
				if (geopoint_xiaolv2_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_xiaolv2_list.get(i));
			}
			break;
		case 3:
			for (int i = 0; i < geopoint_xiaolv3_list.size(); i++) {
				if (geopoint_xiaolv3_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_xiaolv3_list.get(i));
			}
			break;
		case 4:
			for (int i = 0; i < geopoint_xiaolv4_list.size(); i++) {
				if (geopoint_xiaolv4_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_xiaolv4_list.get(i));
			}
			break;
		case 5:
			for (int i = 0; i < geopoint_xiaolv5_list.size(); i++) {
				if (geopoint_xiaolv5_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_xiaolv5_list.get(i));
			}
			break;
		case 6:
			for (int i = 0; i < geopoint_xiaolv6_list.size(); i++) {
				if (geopoint_xiaolv6_list.get(i) != null)
					Map.Public_Static_Map.mapView.getOverlays().remove(geopoint_xiaolv6_list.get(i));
			}
			break;
		default:
			break;
		}

	}

	public Map_Track(GeoPoint gp1, GeoPoint gp2) {
		this.gp1 = gp1;
		this.gp2 = gp2;
	}

	public Map_Track() {
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		Projection projection = mapView.getProjection();
		if (shadow == false) {
			// Paint paint = new Paint();
			// paint.setAntiAlias(true);
			// paint.setColor(Color.BLUE);
			// paint.setStrokeWidth(2);

			// 定义一种线argb(255, 255, 181, 216));
			Paint paint = new Paint();
			paint.setColor(Color.RED);
			paint.setDither(true);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeJoin(Paint.Join.ROUND);
			paint.setStrokeCap(Paint.Cap.ROUND);
			paint.setStrokeWidth(4);

			Point point = new Point();
			projection.toPixels(gp1, point);
			Point point2 = new Point();
			projection.toPixels(gp2, point2);

			canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
					(float) point2.y, paint);
		}
		return super.draw(canvas, mapView, shadow, when);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
	}

	// 计算2点见的距离
	public static boolean Compute_Distance(GeoPoint startPoint,
			GeoPoint finishPoint) // 计算距离 //防止定位跳跃变化
	{
		double lat_startPoint = startPoint.getLatitudeE6() / 1E6;
		double lon_startPoint = startPoint.getLongitudeE6() / 1E6;
		double lat_finishPoint = finishPoint.getLatitudeE6() / 1E6;
		double lon_finishPoint = finishPoint.getLongitudeE6() / 1E6;

		double r = 6378.137;
		lat_startPoint = Math.toRadians(lat_startPoint);
		lon_startPoint = Math.toRadians(lon_startPoint);
		lat_finishPoint = Math.toRadians(lat_finishPoint);
		lon_finishPoint = Math.toRadians(lon_finishPoint);
		double d1 = Math.abs(lat_startPoint - lat_finishPoint);
		double d2 = Math.abs(lon_startPoint - lon_finishPoint);
		double p = Math.pow(Math.sin(d1 / 2), 2) + Math.cos(lat_startPoint)
				* Math.cos(lat_finishPoint) * Math.pow(Math.sin(d2 / 2), 2);
		double dis = r * 2 * Math.asin(Math.sqrt(p));

		BigDecimal a = new BigDecimal(dis);
		BigDecimal result = a.setScale(3, RoundingMode.DOWN); // 截取小数点后3位

		float Distance = result.floatValue();
		if (Distance > 0.5)
			return false; // 突变距离超过500米的 判断为不可用
		return true;
	}
}
